<?php
declare (strict_types = 1);

namespace app\api\controller\ucenter;

use app\api\controller\Api;
use app\common\system\model\UserConfig;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;

class Config extends Api
{
    public function read()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], "参数错误");

        $model = new UserConfig();
        try {
            $result = [];
            $result['detail'] = $model->where('id', $post['id'])->find();
        } catch (DataNotFoundException $e) {
            return success([]);
        }

        return success($result);
    }

    public function save()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], "参数错误");

        try {
            $model = new UserConfig();
            $row = $model->where('id', $post['id'])->find();
            if (!empty($row)) {
                $row->save($post);
            } else {
                $model->save($post);
            }
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        return  success([], "操作成功");
    }
}
